<template>
  <div class="buy">
    <a name="buy"></a>
    <div class="container">
      <Card>
        <div class="wrapper">
          <div class="left text-center">
            <template v-if="active">
              <div class="title" v-text="active.name"></div>

              <div class="amount">
                <span class="currency">￥</span>
                <span class="price" v-text="active.amount">199</span>
              </div>
              <div class="division"></div>

              <div class="description">
                <h2>套餐内容</h2>
                <div>时长：{{ getUnit(active.day) }}</div>


                <div style="margin-top: 10px;">激活限制：
                  <span v-text="getQuantity(active.quantity)"></span>
                </div>

                <BooleanIcon :value="active.canInvoice">电子发票</BooleanIcon>

                <BooleanIcon :value="active.canSupport">服务支持</BooleanIcon>

                <BooleanIcon :value="active.canBussiness">商业使用</BooleanIcon>

                <BooleanIcon :value="active.canUpgrade">在线升级</BooleanIcon>
                <BooleanIcon :value="active.canCustom">付费定制</BooleanIcon>

                <h2>支付方式：</h2>
                <BooleanIcon :value="true">支付宝</BooleanIcon>
                <BooleanIcon :value="true">微信</BooleanIcon>
                <BooleanIcon :value="active.canPublic">对公转账</BooleanIcon>
<!--                <BooleanIcon :value="active.canPrivate">对私转账</BooleanIcon>-->
                <div class="division"></div>
                <div class="help">
                  <i class="el-icon-bell"></i>如遇问题可以加入
                  <el-link @click="groupVisible=true">QQ群</el-link>
                  求助解决
                </div>

              </div>
            </template>
            <template v-else>没有数据</template>
          </div>
          <div class="right">
            <div class="packages">
              <div v-for="item in packages" :key="item.id" :class="{node:true,active:item.active}"
                   @click="select(item)">

                <img class="hot" src="~/static/img/pro_hot.png">
                <div class="selected">
                  <img src="~/static/img/pro_active.png">
                </div>
                <div class="title" v-text="item.name">企业版</div>
                <div class="amount">
                  <span class="currency">￥</span>
                  <span class="price" v-text="item.amount">199</span>
                </div>
                <div class="day" v-text="getUnit(item.day)"></div>
                <div class="division"></div>
                <div class="quantity">
                  <span v-text="getQuantity(item.quantity)"></span>
                  <el-popover
                    placement="top-start"
                    title="设备数量说明"
                    width="200"
                    trigger="click"
                    content="一台设备是指服务器、个人电脑等硬件设备，单台设备上不限制项目数量，多次激活只算一台设备的激活数。">
                    <el-button type="text" slot="reference"><i class="el-icon-warning-outline"></i></el-button>
                  </el-popover>
                </div>
              </div>

            </div>
            <div class="division"></div>

            <div class="text-center">
              <el-button type="success" :icon="qrcodeVisible?'el-icon-loading':'el-icon-bank-card'"
                         :disabled="qrcodeVisible" @click="qrcodeVisible=true">立即支付
              </el-button>
            </div>
          </div>
        </div>
      </Card>
    </div>

    <ChatGroup :visible.sync="groupVisible" @close="groupVisible=false"></ChatGroup>

    <QrcodeDialog @login="$emit('login')" :visible.sync="qrcodeVisible" @close="qrcodeVisible=false"
                  :package="active"></QrcodeDialog>

  </div>
</template>

<script>
import BooleanIcon from "@/components/simplepro/BooleanIcon";
import QrcodeDialog from "@/components/simplepro/QrcodeDialog";

export default {
  name: "SimpleProBuy",
  components: {BooleanIcon, QrcodeDialog},
  props: {
    packages: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data() {
    return {
      active: null,
      groupVisible: false,
      qrcodeVisible: false
    }
  },
  created() {
    if (this.packages.length !== 0) {
      let item = this.packages[0];
      this.select(item);
    }
  },
  methods: {
    getUnit(day) {
      if (day === 30) {
        return '1月'
      } else if (day === 365) {
        return '1年'
      } else if (day <= 0) {
        return '永久'
      } else {
        return day + '天'
      }
    },
    select(item) {
      this.packages.forEach(item => item.active = false);
      item.active = true;
      this.active = item;
    },
    getQuantity(quantity) {
      if (quantity <= 0 || quantity >= 10000) {
        return '不限制设备';
      } else {
        return `可激活${quantity}台设备`;
      }
    }
  }
}
</script>

<style scoped lang="less">
.wrapper {
  display: flex;

  .left {
    flex: 0 0 275px;
    border-right: 1px solid var(--border-color);

    .description {
      text-align: left;
    }
  }

  .right {
    flex: 1;
  }

  .amount {
    margin-top: 20px;
    margin-bottom: 20px;
    color: #f0a854;

    .currency {
      font-size: 24px;
      font-weight: bold;
    }

    .price {
      font-size: 38px;
      font-weight: bold;
    }

  }

  .day {
    font-size: 14px;
    color: var(--post-color);
  }

  .division {
    border-top: 1px solid var(--border-color);
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .packages {
    display: flex;
    flex-flow: row wrap;
    align-content: flex-start;

    .node {
      text-align: center;
      flex: 0 0 180px;
      padding: 30px 6px;
      margin: 10px;
      border: 1px solid var(--border-color);

      position: relative;
      border-radius: 6px;
      cursor: pointer;

      transition: all 0.3s;

      .title {
        font-size: 16px;
        color: var(--color);
        margin-bottom: 5px;
      }

      .hot {
        position: absolute;
        right: -7px;
        top: -12px;
      }

    }

    .quantity {
      color: var(--post-color);
      font-size: 14px;
    }

    .selected {
      position: absolute;
      left: 0;
      top: 0;
      display: none;
    }

    .active {
      background-color: #fff7ea;
      border: 1px solid #eb9f4c;

      .selected {
        display: block;
      }
    }
  }


}

.help {
  margin-top: 20px;

  a {
    color: var(--color) !important;
  }
}

</style>
